package com.sj.uabrowser.mvp.main.loader;

import com.sj.uabrowser.framework.BaseLoader;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.main.bean.MainNewFuncs;
import com.sj.uabrowser.net.AuthResultClient;
import com.sj.uabrowser.net.ServiceApi;
import com.sj.uabrowser.net.bean.MainFunc;
import com.sj.uabrowser.net.bean.NewsTabItem;
import com.sj.uabrowser.net.bean.PageBean;

import java.util.TreeMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class FuncsLoader extends BaseLoader {

    public FuncsLoader(BasePresenter mPresenter) {
        super(mPresenter);
    }

    public void requestFuncs(final int requestId, TreeMap<String,Object>params) {
        AuthResultClient.getServiceMainNewFuncs(ServiceApi.class)
                .GetMainFuncs()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MainNewFuncs>() {
                    @Override
                    public void accept(MainNewFuncs mainFunc) throws Exception {
                        if(mPresenter!=null) {
                            mPresenter.onDataCallBack(requestId, mainFunc);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId,throwable);
                    }
                });
    }

    public void fetchTabList(final int requestId,TreeMap<String,Object>params){
        AuthResultClient.getService2(ServiceApi.class)
                .GetTabList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PageBean<NewsTabItem>>() {
                    @Override
                    public void accept(PageBean<NewsTabItem> tabItems) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBack(requestId,tabItems);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(mPresenter!=null)
                            mPresenter.onDataCallBackError(requestId,throwable);
                    }
                });
    }

}
